import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import hilog from '@ohos.hilog';
import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
import dmrnapi from "@ohos.dmrnapi";

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    globalThis.abilityContext = this.context;
    let parameters = want.parameters
    console.log(`EntryAbility onCreate Want_Parameters : ${JSON.stringify(parameters)}`);
    let ip :string= JSON.stringify(want.parameters?.clientIp)
    console.log(`EntryAbility onCreate Want_IP : ${JSON.stringify(ip)}`);
    AppStorage.setOrCreate('IP', ip);
    AppStorage.setOrCreate('IsTrust', false);
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy and dlnaplayer stop');
    dmrnapi.Stop();
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    AppStorage.setOrCreate('window', windowStage);
    this.requestPermissionsFn();
    let windowClass: window.Window | null = null;
    windowStage.getMainWindow((err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
        return;
      }
      windowClass = data;
      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
      try {
        let properties: window.WindowProperties = windowClass.getWindowProperties();
        console.info('Succeeded to obtain the window properties. windowRect: ' + JSON.stringify(properties.windowRect));
        AppStorage.setOrCreate('windowsize', properties);

      } catch (exception) {
        console.error('Failed to obtain the window properties. Cause: ' + JSON.stringify(exception));
      }
    })
    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
      let color: string = '#00000000';
      try {
        if (windowClass != null) {
          windowClass.setWindowBackgroundColor(color);
        }
      } catch (exception) {
        console.error('Failed to set the background color. Cause: ' + JSON.stringify(exception));
      }
      ;
    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }

  requestPermissionsFn(): void {
    let atManager = abilityAccessCtrl.createAtManager();
    atManager.requestPermissionsFromUser(globalThis.abilityContext, [
      'ohos.permission.GET_NETWORK_INFO',
      'ohos.permission.CONNECTIVITY_INTERNAL',
      'ohos.permission.NETSYS_INTERNAL',
      'ohos.permission.MANAGE_NET_STRATEGY',
      'ohos.permission.INTERNET'
    ]).then((): void => {
      AppStorage.SetOrCreate<boolean>('isShow', true);
      hilog.info(0x0000, 'testTag', '%{public}s', 'request Permissions success!');
    }).catch((err: BusinessError) => {
      //Logger.info(TAG, `requestPermissionsFromUser call Failed! error: ${error.code}`);
      hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', err ?? '');
    });
  }
}
